
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // 启动要显示的界面
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("图标组件示例"),
      ),
      body: JKCameraScreen(),
    );
  }
}

class JKCameraScreen extends StatefulWidget {
  @override
  _JKCameraScreenState createState() => _JKCameraScreenState();
}

class _JKCameraScreenState extends State<JKCameraScreen> {
  PickedFile _imageFile;
  final ImagePicker _picker = ImagePicker();

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        children: [
          RaisedButton(
              child: Text('选择一个相册'),
              onPressed: _pickImage
          ),
          _imageFile == null ? Text('请选择一张照片') : Image.file(File(_imageFile.path))
        ],
      ),
    );
  }

  void _pickImage() async {
    print('选择相册');
    PickedFile pickedFile = await _picker.getImage(source: ImageSource.gallery);
    setState(() {
      _imageFile = pickedFile;
    });
  }
}

